/**
 * Created by Administrator on 2016/3/2.
 */
gpNamespace.UserInfoLayer = cc.Layer.extend({
	infoBackground:null,
	userHeadBg2:null,
	sexOn:null,
	vipList:null,
	userName:null,
	winRate:null,
	sex:null,
	ctor: function () {
		this._super();

		this.init();
	},
	init: function () {
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkuserinforRes.Pdkuserinfor_plist);
		var size = cc.winSize;

		//背景图层
		var background = new cc.Sprite(gpNamespace.userInfo.User_Bg);
		background.attr({
			x: 110 + background.width / 2,
			y: 52 + background.height / 2,
			anchorX: 0.5,
			anchorY: 0.5
		});
		this.addChild(background);

		//用户信息背景图层
		var infoBackground = new cc.Sprite(gpNamespace.userInfo.User_Info_Bg);
		infoBackground.attr({
			x: 140 + infoBackground.width / 2,
			y: 132 + infoBackground.height / 2,
			anchorX: 0.5,
			anchorY: 0.5
		});
		this.addChild(infoBackground);
		this.infoBackground = infoBackground;

		//个人信息
		var infoOn = new cc.MenuItemImage(gpNamespace.userInfo.User_Info_On);
		var infoOff = new cc.MenuItemImage(gpNamespace.userInfo.User_Info_Off);
		var info = new cc.MenuItemToggle(infoOff, infoOn, this.infoCallbackFunction, this);
		info.setSelectedIndex(1);
		this.info=info;
		info.setUserData(this);
		//vip特权
		var vipOn = new cc.MenuItemImage(gpNamespace.userInfo.User_Vip_On);
		var vipOff = new cc.MenuItemImage(gpNamespace.userInfo.User_Vip_Off);
		var vip = new cc.MenuItemToggle(vipOff, vipOn, this.vipCallbackFunction, this);
		this.vip=vip;
		vip.setUserData(this);

		var TopListMenu = new cc.Menu(info, vip);
		TopListMenu.attr({
			x: size.width/2,
			y: 530+vip.height/2
		});
		TopListMenu.alignItemsHorizontallyWithPadding(330);
		this.addChild(TopListMenu,3);

		//用于扩大页签点击范围--------------------------------------------------------------------------------------
		var infoTouchItem =new cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(),this.infoCallbackFunction,this);
		infoTouchItem.setUserData(this);
		infoTouchItem.setContentSize(cc.size(485, 85));
		infoTouchItem.setAnchorPoint(0.5, 0.5);
		infoTouchItem.setPosition(390,565);

		var vipTouchItem =new cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(),this.vipCallbackFunction,this);
		vipTouchItem.setUserData(this);
		vipTouchItem.setContentSize(cc.size(485, 85));
		vipTouchItem.setAnchorPoint(0.5, 0.5);
		vipTouchItem.setPosition(890,565);
		var touchMenu = new cc.Menu(infoTouchItem,vipTouchItem);
		touchMenu.attr({
			x:0,
			y:0,
			anchorX:0,
			anchorY:0
		});
		this.addChild(touchMenu,5);

		//--------------------------------------------------------------------------------------用于扩大页签点击范围
		//左边页签背景
		var userBg = new cc.Sprite(gpNamespace.userInfo.User_Info_OnBg);
		userBg.attr({
			x: 120 + userBg.width / 2,
			y: 510 + userBg.height / 2,
		});
		this.userBg=userBg;
		this.addChild(userBg,2);

		//头像框背景
		var userHeadBg2 = new cc.Sprite(gpNamespace.userInfo.User_Info_headBg2);
		userHeadBg2.attr({
			x: 40+userHeadBg2.width / 2,
			y: 100+userHeadBg2.height / 2,
		});
		this.userHeadBg2=userHeadBg2;
		infoBackground.addChild(userHeadBg2,2);

		//头像框背景小
		var userHeadBg1 = new cc.Sprite(gpNamespace.userInfo.User_Info_headBg1);
		userHeadBg1.attr({
			x: userHeadBg2.getPositionX()-2,
			y: userHeadBg2.getPositionY()+7,
		});
		this.userHeadBg1=userHeadBg1;
		infoBackground.addChild(userHeadBg1,2);

		//成为vip
		var toBeVip = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.userInfo.User_ToBeVip),
				new tyNamespace.maskSprite(gpNamespace.userInfo.User_ToBeVip),
				this.tobeVipCallbackFunction, this);
		toBeVip.setPosition(-480,-300);

		var toBeVipMenu = new cc.Menu(toBeVip);
		infoBackground.addChild(toBeVipMenu,1);

		var userName = new cc.LabelTTF("昵称： "+ userInfo.nickName, gpNamespace.DEFAULTFONT, 30);
		userName.attr({
			x: 350,
			y: 320,
			anchorX:0,
			anchorY:0.5

		});
		this.userName = userName;
		infoBackground.addChild(userName,3);
		var isVip = userInfo.isVip;
		if(isVip){
			var vipTime = new cc.LabelTTF("(剩余VIP天数: "+userInfo.vipDays+"天)",gpNamespace.DEFAULTFONT,28);
			vipTime.attr({
				x:370+this.userName.width,
				y:320,
				anchorX:0,
				anchorY:0.5
			});
			infoBackground.addChild(vipTime)
		}
//		console.log("username:"+userName.getString()+ userInfo.nickName);
		var gameTimes = userInfo.shengCount+userInfo.shuCount;
		var win = gameTimes == 0 ? 0 : ((userInfo.shengCount/gameTimes)*100).toPrecision(3);

		var winRate = new cc.LabelTTF("胜率： " + userInfo.shengCount + "胜/" + userInfo.shuCount + "负/" +  "("
				+ (win + "%") 
				+")" , gpNamespace.DEFAULTFONT, 30);
		winRate.attr({
			x: 350,
			y: 255,
			anchorX:0,
			anchorY:0.5
		});
		infoBackground.addChild(winRate,3);	
		this.winRate = winRate;	

		//性别单选
		var maleBg= new cc.MenuItemImage(gpNamespace.userInfo.User_Info_maleBg,gpNamespace.userInfo.User_Info_maleBg,this.maleCallbackFunction, this);
		maleBg.attr({
			x: 450,
			y: 200
		});

		var femaleBg = new cc.MenuItemImage(gpNamespace.userInfo.User_Info_femaleBg,gpNamespace.userInfo.User_Info_femaleBg,this.femaleCallbackFunction, this);
		femaleBg.attr({
			x: 700,
			y: 200
		});

		//性别单选菜单
		var menu = new cc.Menu(maleBg, femaleBg);
		menu.x = 0;
		menu.y = 0;
		menu.anchorX = 0;
		menu.anchorY = 0;
		infoBackground.addChild(menu);

		//选中按钮图标
		var sexOn = new cc.Sprite(gpNamespace.userInfo.User_Info_SexOn);
		sexOn.attr({
			x: 530,
			y: 199
		});
		this.sexOn=sexOn;
		infoBackground.addChild(sexOn,2);


		var maleLabel=new cc.LabelTTF("男性发音", gpNamespace.DEFAULTFONT, 28);
		maleLabel.attr({
			x: 450, 
			y: 200
		});
		infoBackground.addChild(maleLabel);

		var femaleLabel=new cc.LabelTTF("女性发音", gpNamespace.DEFAULTFONT, 28);
		femaleLabel.attr({
			x: 700, 
			y: 200
		});
		infoBackground.addChild(femaleLabel);

		//金币数
		var gold = new tyNamespace.NumLayer(userInfo.gold,19,0,"sz_cdd_js_");
		gold.attr({
			x:480,
			y:138,
			anchorX:0,
			anchorY:0.5
		});
		infoBackground.addChild(gold,2);	

		//钻石数
		var diamond = new tyNamespace.NumLayer(userInfo.diamond,19,0,"sz_cdd_js_");
		diamond.attr({
			x:gold.getPositionX(),
			y:gold.getPositionY()-50,
			anchorX:0,
			anchorY:0.5
		});
		infoBackground.addChild(diamond,2);	

		//兑换券数
		var voucher = new tyNamespace.NumLayer(userInfo.voucher,19,0,"sz_cdd_js_");
		voucher.attr({
			x:gold.getPositionX(),
			y:gold.getPositionY()-100,
			anchorX:0,
			anchorY:0.5
		});
		infoBackground.addChild(voucher,2);	

		//右边页签背景
		var vipBg = new cc.Sprite(gpNamespace.userInfo.User_Vip_OnBg);
		vipBg.attr({
			x: 638 + vipBg.width / 2,
			y: 510 + vipBg.height / 2,
		});
		this.vipBg=vipBg;
		this.addChild(vipBg,2);
		vipBg.visible=false;



		//vip特权
		var vipList = new tableViewPro.TableView(this, cc.size(980, 380), 0);
		vipList.setPosition(170, 120);
		vipList.setAnchorPoint(0, 0);
		this.addChild(vipList);
		vipList.setBarOffset(0, 0); 

		vipList.clearData();
		vipList.addData({
			"barInfo": {"width": 2, "height": 80},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 2},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": 7,
			            		 "imageName": "",
			            		 "height": 175
			            	 }
			             }
			             ]
		});
		vipList.showData();
		vipList.visible=false;
		vipList.setTouchEnabled(false);
		this.vipList=vipList;

		//cells上加表现
		var cellCount = vipList.currentItem.cellInfo.cellCount;
		var cellType = vipList.currentItem.cellInfo.cellType;
		var currentPage = vipList.currentPage;
		for (var a = 0; a < cellCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
			case 1://menu
//				cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
				break;
			}
			this.setvipListkCell(a, cell);
		}

		//加载用户本地记录声音性别
		var soundsex = cc.sys.localStorage.getItem(gpNamespace.SoundSex);
		if(soundsex){
			if(soundsex == "男"){
				this.maleCallbackFunction();
//				cc.log("localsoundSex:"+LocalsoundSex);
			}else{
				this.femaleCallbackFunction();
//				cc.log("localsoundSex:"+LocalsoundSex);
			}
			this.sex = soundsex;
		}

		this.setUserHead(gpNamespace.QQInfo.HEADURL);//加载用户头像

	},

	setvipListkCell:function(index, cell){
		imagePath = "#pdk_grxx_0"+(13+index)+"u.png";
		var discribeTitle = new cc.Sprite(imagePath);
		discribeTitle.attr({
			x: 0,
			y: 0,
			anchorX:0,
			anchorY:0
		});
		cell.addChild(discribeTitle);
	},

	//设置性别选择触发事件
	maleCallbackFunction:function(){
		this.sexOn.x = 530;
		this.sexOn.y = 199;
		this.sex = "男";
	},

	femaleCallbackFunction:function(){
		this.sexOn.x = 779;
		this.sexOn.y = 198;
		this.sex = "女";
	},

	infoCallbackFunction:function(sender){
//		tyNamespace.setMenuEnable(sender);
		var self = sender.getUserData();
		self.userBg.visible = true;
		self.vipBg.visible = false;
		self.info.setSelectedIndex(1);
		self.vip.setSelectedIndex(0);

		self.infoBackground.visible=true;
		self.vipList.visible=false;
		self.vipList.setTouchEnabled(false);
	},
	vipCallbackFunction:function(sender){
//		tyNamespace.setMenuEnable(sender);
		var self = sender.getUserData();
		self.userBg.visible = false;
		self.vipBg.visible = true;
		self.info.setSelectedIndex(0);
		self.vip.setSelectedIndex(1);

		self.infoBackground.visible=false;
		self.vipList.visible=true;
		self.vipList.setTouchEnabled(true);
	},
	tobeVipCallbackFunction:function(sender){
		cc.log("成为vip显示");
		tyNamespace.setMenuEnable(sender);
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer(70);
		modelLayer.setTouchArea(cc.size(1060, 610), cc.p(115, 45));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 10);
		var cwvipLayer = new gpNamespace.CwvipLayer();
		cwvipLayer.attr({
			//左上角坐标
			x: 0,
			y: cc.winSize.height,
		});
		modelLayer.addChild(cwvipLayer);
		tyNamespace.AnimationManager.nodeJumpShow(cwvipLayer, -cc.winSize.height);
	},

	addUserHead: function (texture) {
		var imgSprite;
		if (!texture) {
			imgSprite = new cc.Sprite(gpNamespace.xinzeng.QQ);
		}
		else {
			imgSprite = new cc.Sprite();
			imgSprite.initWithTexture(texture);
		}
		imgSprite.setScale(185 / imgSprite.getContentSize().width, 199 / imgSprite.getContentSize().height);
		imgSprite.setAnchorPoint(0, 0);
		imgSprite.setPosition(0, 0);
		imgSprite.setName("userHead");
		this.userHeadBg1.addChild(imgSprite,0);
	},

	setUserHead: function (headImgUrl) {
		var userHead = this.getChildByName("userHead");
		userHead && userHead.removeFromParent(true);

		tyNamespace.loadImgByUrl(headImgUrl, this.addUserHead, this);
		//xzgbNamespace.loadImgByUrl("http://q.qlogo.cn/g?b=qq&nk=1109980012&s=100", this.addUserHead, this);
	},

	onExit:function(){
		this._super();
		cc.sys.localStorage.setItem(gpNamespace.SoundSex, this.sex);//设置本地
		xinyou.netManager.changeUserInfo(userInfo.headImageID, userInfo.nickName, this.sex);//设置服务器
	}
});